package net.wendal.luat.air720;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.nutz.lang.Files;
import org.nutz.lang.Lang;

public class WebDataBuilder {
    
    protected byte[] header = new byte[16+4];
    protected List<WebDataFile> files = new ArrayList<>();
    protected byte[] ender = new byte[4];
    
    class WebDataFile implements Comparable<WebDataFile> {
        byte[] header = new byte[8];
        byte[] _name = new byte[128];
        byte[] data;
        
        String name;
        int size;
        int fixSize;
        
        @Override
        public int compareTo(WebDataFile o) {
            if (this.size > o.size)
                return -1;
            else if (this.size < o.size)
                return 1;
            return 0;
        }
    }
    
    public void read(File f) throws IOException {
        RandomAccessFile raf = new RandomAccessFile(f, "r");
        raf.readFully(header);
        while (true) {
            if (f.length() - raf.getFilePointer() == 4) {
                break;
            }
            WebDataFile wdf = new WebDataFile();
            raf.readFully(wdf.header);
            raf.readFully(wdf._name);
            wdf.name = new String(wdf._name).trim();
            //System.out.println("file header dump: " + Lang.fixedHexString(wdf.header).toUpperCase());
            wdf.fixSize = wdf.header[7] & 0xFF;
            int vhigh = wdf.header[6] & 0xFF;
            int high = wdf.header[5] & 0xFF;
            int low = wdf.header[4] & 0xFF;
            wdf.size = vhigh*256*256 + high * 256 + low;
            System.out.println("name=" + wdf.name + ", size=" + wdf.size +", " + Lang.fixedHexString(wdf.header).toUpperCase());
            wdf.data = new byte[wdf.size];
            raf.readFully(wdf.data);
            files.add(wdf);
            // 修正实际大小
            wdf.size -= wdf.fixSize;
        }
        raf.readFully(ender);
        raf.close();
        
        Collections.sort(this.files);
    }
    
    public void doPrint() {
        for (WebDataFile wdf : files) {
            System.out.printf("%-60s%6s\r\n", wdf.name, wdf.size);
        }
        System.out.println("Count = " + files.size() + "  " + Integer.toHexString(files.size()).toUpperCase());
    }
    
    public void doBuild(File f) throws IOException {
        List<WebDataFile> files2 = new ArrayList<>();
        for (WebDataFile wdf : files) {
            if (wdf.name.endsWith(".cgi")) {
                files2.add(wdf);
            }
            else if (wdf.name.endsWith(".xml")) {
                files2.add(wdf);
            }
            else if (wdf.name.endsWith(".htdigest")) {
                files2.add(wdf);
            }
            else if (wdf.name.endsWith(".txt")) {
                files2.add(wdf);
            }
        }
        int size = 0;
        for (WebDataFile wdf : files2) {
            size += wdf.size;
        }
        size += header.length + ender.length;
        System.out.println("精简后的大小 "+size);
        try (FileOutputStream fos = new FileOutputStream(f)) {
            fos.write(header);
            for (WebDataFile wdf : files2) {
                fos.write(wdf.header);
                fos.write(wdf._name);
                fos.write(wdf.data);
            }
            fos.write(ender);
            fos.flush();
        }
    }
    
    public void exportFileData(String path) throws IOException {
        for (WebDataFile wdf : files) {
            String dst = path + "/" + wdf.name;
            Files.createFileIfNoExists(dst);
            try (FileOutputStream fos = new FileOutputStream(dst)) {
                fos.write(wdf.data, 0, wdf.size);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        WebDataBuilder builder = new WebDataBuilder();
        builder.read(new File("J:\\N插座的资料\\webdata\\WebData_Big.bin"));
        builder.doPrint();
        builder.doBuild(new File("J:\\N插座的资料\\webdata\\WebData.bin"));
        builder.exportFileData("J:\\N插座的资料\\webdata_export\\");
    }
}
